/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.samples.customtype;

import axescon.xacml.model.shared.attr.AbstractAttributeValue;
import axescon.xacml.model.shared.attr.ArithmeticValue;

import java.math.BigInteger;


/**
 * demonstrates how to add custom types. this type is based on BigInteger type.
 */
public class BigIntegerAttribute extends AbstractAttributeValue implements ArithmeticValue {
    public static final String dataType =
            "http://www.w3.org/2001/XMLSchema#bigInteger";

    public String getDataType() {
        return dataType;
    }

    private BigInteger value;

    public BigIntegerAttribute(BigInteger value) {
        this.value = value;
    }

    public BigInteger getValue() {
        return value;
    }

    public boolean equals(Object o) {
        if (!(o instanceof BigIntegerAttribute))
            return false;

        BigIntegerAttribute other = (BigIntegerAttribute) o;

        return (value == other.value);
    }

    public int hashCode() {
        return (int) value.intValue();
    }

    public ArithmeticValue subtract(ArithmeticValue arg) {
        BigIntegerAttribute dv = (BigIntegerAttribute) arg;
        return new BigIntegerAttribute(getValue().add(dv.getValue().negate()));
    }

    public ArithmeticValue multiply(ArithmeticValue arg) {
        BigIntegerAttribute dv = (BigIntegerAttribute) arg;
        return new BigIntegerAttribute(getValue().multiply(dv.getValue()));
    }

    public ArithmeticValue divide(ArithmeticValue arg) {
        BigIntegerAttribute dv = (BigIntegerAttribute) arg;
        return new BigIntegerAttribute(getValue().divide( dv.getValue()));
    }

    public ArithmeticValue mod(ArithmeticValue arg) {
        BigIntegerAttribute dv = (BigIntegerAttribute) arg;
        return new BigIntegerAttribute(getValue().mod(dv.getValue()));
    }

    public ArithmeticValue add(ArithmeticValue arg) {
        BigIntegerAttribute dv = (BigIntegerAttribute) arg;
        return new BigIntegerAttribute(getValue().add( dv.getValue()));
    }

    public ArithmeticValue sum(ArithmeticValue[] arg) {
        BigInteger sum =  BigInteger.ZERO;
        if (arg != null) {
            for (int i = 0; i < arg.length; i++) {
                sum = sum.add(((BigIntegerAttribute) arg[i]).getValue());
            }
        }
        return new BigIntegerAttribute(sum);
    }

    public int compareTo1(axescon.xacml.model.policy.AttributeValue o) {
        BigIntegerAttribute obj = (BigIntegerAttribute) o;
        return getValue().compareTo( obj.getValue());
    }

    public ArithmeticValue getZero() {
        return ZERO;
    }

    protected static final BigIntegerAttribute ZERO = new BigIntegerAttribute(BigInteger.ZERO);
}
